
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO
Imports _2bMatesSite.Common


Namespace _2bMatesSite
	''' <summary>
	''' Summary description for upload_song.
	''' </summary>
	Public Partial Class upload_song
		Inherits BaseWebPage

		Protected PicturesTable As System.Web.UI.WebControls.Table
		Protected lblForbiddenUpload As System.Web.UI.WebControls.Label

		Protected Const IMAGES_ON_ROW As Integer = 3
		Protected Const MAX_FILE_SIZE As Integer = 5242880
		' in bytes
		Protected Const MAX_TOTAL_FILE_SIZE As Integer = 7340032
		' in bytes
		Protected heightString As String = ""
		Protected htPageConstants As New Hashtable()

		Public Function CreateDataSource() As ICollection
			Dim usr As BO.User = LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If


			Dim songsCount As Integer = 0

			If usr.SongURL1 <> Nothing AndAlso usr.SongURL1 <> "" Then
				songsCount += 1
			End If
			If usr.SongURL2 <> Nothing AndAlso usr.SongURL2 <> "" Then
				songsCount += 1
			End If
			If usr.SongURL3 <> Nothing AndAlso usr.SongURL3 <> "" Then
				songsCount += 1
			End If

			If songsCount = 0 Then
				lblNoImage.Visible = True
			Else
				lblNoImage.Visible = False
			End If

			If songsCount = Constants.MAX_MEDIA_UPLOADS Then
				lblName.Visible = False
				txtSongName.Visible = False
				fldFileUpload.Visible = False
				btnUpload.Visible = False
				lblMessage.Visible = True
ElseIf songsCount < Constants.MAX_MEDIA_UPLOADS Then
				lblName.Visible = True
				txtSongName.Visible = True
				fldFileUpload.Visible = True
				btnUpload.Visible = True
				lblMessage.Visible = False
			End If

			Dim dt As New DataTable()
			Dim dr As DataRow

			dt.Columns.Add(New DataColumn("SongURL", GetType(String)))
			dt.Columns.Add(New DataColumn("SongName", GetType(String)))
			'dt.Columns.Add(new DataColumn("AddEditDescriptionText", typeof(string)));		
			dt.Columns.Add(New DataColumn("PlayMusicURL", GetType(String)))
			'dt.Columns.Add(new DataColumn("Comments", typeof(string)));	
			dt.Columns.Add(New DataColumn("SongID", GetType(String)))

			If usr.SongURL1 <> Nothing AndAlso usr.SongURL1 <> "" Then
				dr = dt.NewRow()

				dr(0) = usr.SongURL1
				dr(1) = usr.SongURLName1
				dr(2) = Constants.PLAY_MUSIC + "?filename=" + Server.UrlEncode(dr(0).ToString())
				dr(3) = "1"

				dt.Rows.Add(dr)
			End If

			If usr.SongURL2 <> Nothing AndAlso usr.SongURL2 <> "" Then
				dr = dt.NewRow()

				dr(0) = usr.SongURL2
				dr(1) = usr.SongURLName2
				dr(2) = Constants.PLAY_MUSIC + "?filename=" + Server.UrlEncode(dr(0).ToString())
				dr(3) = "2"

				dt.Rows.Add(dr)
			End If

			If usr.SongURL3 <> Nothing AndAlso usr.SongURL3 <> "" Then
				dr = dt.NewRow()

				dr(0) = usr.SongURL3
				dr(1) = usr.SongURLName3
				dr(2) = Constants.PLAY_MUSIC + "?filename=" + Server.UrlEncode(dr(0).ToString())
				dr(3) = "3"

				dt.Rows.Add(dr)
			End If

			Dim dv As New DataView(dt)
			Return dv
		End Function

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here

			' An example displaying the first picture
			' this.pageTemplate.PageTitle = PageTitles.UPLOAD_SONG_PAGE;
			Dim usr As User = LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			Me.initPageTitles(Constants.UPLOAD_SONG, "", uploadImageForm, Nothing, htPageConstants)
			Me.initAdvertisements(uploadImageForm, Nothing)

			Dim totalPicturesLength As Integer = usr.GetPicturesTotalLength()

			'if ( totalPicturesLength >= MAX_TOTAL_FILE_SIZE )
			'{
			'	fldFileUpload.Disabled = true;
			'	lblForbiddenUpload.Text = "You have are not allowed to upload more than 500K total image size.";
			'	lblForbiddenUpload.Visible = true;
			'}

			Dim regParam As String = Request.Params(Constants.REGISTRATION_PARAM)
			litSpacer.Text = "<IMG height='8' alt='' src='images/spacer.gif' width=1 border=0 >"

			If regParam <> Nothing AndAlso regParam <> "" AndAlso Convert.ToInt32(regParam) = Constants.REGISTRATION_VALUE Then
				btnSkip.Visible = True
				' this.pageTemplate.PageTitle = PageTitles.JOIN_PAGE;
				If usr.GetPictures().Count = 0 Then
					tblPhotos.Visible = False
					litSpacer.Visible = True
				Else
					btnSkip.Text = Me.GetPageConstText(htPageConstants, "CONST_Next_TEXT")
				End If
				heightString = "height='100%'"
				litSpacer.Visible = False
			End If

			If Not IsPostBack Then
				BindList()
			End If
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.dlPictures.ItemCommand, AddressOf Me.dlPictures_ItemCommand

		End Sub
#End Region

		Public Sub BindList()
			dlPictures.DataSource = CreateDataSource()
			dlPictures.DataBind()
		End Sub

		Public Sub dlPictures_ItemCommand(ByVal Sender As Object, ByVal e As DataListCommandEventArgs)
			Dim src As Object = e.CommandSource
			Dim cmd As String = ""
			Dim usr As BO.User = LoggedUser
			Dim type As String = src.[GetType]().ToString()
			Select Case type
				Case "System.Web.UI.WebControls.ImageButton"
					cmd = (DirectCast(src, ImageButton)).CommandName
					Exit Select
				Case "System.Web.UI.WebControls.LinkButton"
					cmd = (DirectCast(src, LinkButton)).CommandName
					Exit Select
				Case "System.Web.UI.WebControls.Button"
					cmd = (DirectCast(src, Button)).CommandName
					Exit Select
			End Select
			Select Case cmd
				Case "SetDefault"
					'usr.DefaultPictureID = Convert.ToInt32(e.CommandArgument);
					'usr.Save();
					Exit Select
				Case "ViewLargerPhotoLink"
					'Response.Redirect(Constants.VIEW_PHOTO_ALBUM_COMMENTS_PAGE+"?img="+e.CommandArgument.ToString()+"&usr="+LoggedUser.ID);
					Exit Select
				Case "Remove"
					'Response.Redirect(Constants.DELETE_PHOTO + "?id=" + e.CommandArgument.ToString());
					usr.DeleteSong(e.CommandArgument.ToString(), Page.Server.MapPath(".") + "\uploads\")
					Exit Select
				Case "ViewComments"
					'Response.Redirect(Constants.VIEW_PHOTO_ALBUM_COMMENTS_PAGE+"?img="+e.CommandArgument+"&usr="+LoggedUser.ID);
					Exit Select
				Case "AddDescription"
					'Response.Redirect(Constants.ADD_PHOTO_DESCRIPTION+"?id="+e.CommandArgument);
					Exit Select
			End Select
			BindList()
		End Sub

		Protected Sub validateFile(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
			Dim usr As User = Me.LoggedUser

			If (fldFileUpload.PostedFile <> Nothing) AndAlso (fldFileUpload.PostedFile.ContentLength > 0) Then

				Dim fileName As String = args.Value.Trim()
				Dim fileNameInLowerCase As String = fileName.ToLower()
				Dim fileSize As Integer = fldFileUpload.PostedFile.ContentLength

				If fileNameInLowerCase.EndsWith(".wav") OrElse fileNameInLowerCase.EndsWith(".wma") OrElse fileNameInLowerCase.EndsWith(".mp3") OrElse fileNameInLowerCase.EndsWith(".avi") OrElse fileNameInLowerCase.EndsWith(".mpg") OrElse fileNameInLowerCase.EndsWith(".mpeg") Then
					If fileSize > MAX_FILE_SIZE Then
						valFileFormat.ErrorMessage = Me.GetPageConstText(htPageConstants, "CONST_MsgErrMaxFileSize_TEXT")
						args.IsValid = False
					End If
				Else
					args.IsValid = False
				End If
			Else
				args.IsValid = False
			End If
		End Sub

		Protected Sub UploadImage(ByVal file As HttpPostedFile, ByVal usr As User)
			Dim splitFileName As String() = file.FileName.Split(New Char() {"/"C, "\"C})
			Dim sFileName As String = System.IO.Path.GetFileName(file.FileName)
			Dim sFileExtension As String = sFileName.Substring(sFileName.LastIndexOf("."))
			Dim bMachFound As Boolean = False

			If bMachFound = False AndAlso (usr.SongURL1 = Nothing OrElse usr.SongURL1 = "") Then
				sFileName = usr.ID.ToString() + "_song1" + sFileExtension
				usr.SongURL1 = sFileName
				usr.SongURLName1 = txtSongName.Text
				bMachFound = True
			End If

			If bMachFound = False AndAlso (usr.SongURL2 = Nothing OrElse usr.SongURL2 = "") Then
				sFileName = usr.ID.ToString() + "_song2" + sFileExtension
				usr.SongURL2 = sFileName
				usr.SongURLName2 = txtSongName.Text
				bMachFound = True
			End If

			If bMachFound = False AndAlso (usr.SongURL3 = Nothing OrElse usr.SongURL3 = "") Then
				sFileName = usr.ID.ToString() + "_song3" + sFileExtension
				usr.SongURL3 = sFileName
				usr.SongURLName3 = txtSongName.Text
				bMachFound = True
			End If

			If bMachFound = True Then
				Dim sSaveLocation As String = Page.Server.MapPath(".") + "\uploads\" + sFileName

				file.SaveAs(sSaveLocation)

				usr.Save()
			End If
		End Sub

		Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			If Page.IsValid Then
				Dim usr As BO.User = LoggedUser
				UploadImage(fldFileUpload.PostedFile, usr)

				tblPhotos.Visible = True
				Dim regParam As String = Request.Params(Constants.REGISTRATION_PARAM)
				If regParam <> Nothing AndAlso regParam <> "" AndAlso Convert.ToInt32(regParam) = Constants.REGISTRATION_VALUE Then
					btnSkip.Visible = True
					btnSkip.Text = Me.GetPageConstText(htPageConstants, "CONST_Next_TEXT")
					Response.Redirect(Constants.UPLOAD_SONG + "?" + Constants.REGISTRATION_PARAM + "=" + Constants.REGISTRATION_VALUE)
				Else
					Response.Redirect(Constants.UPLOAD_SONG)
				End If
			End If
		End Sub

		Private Sub btnSkip_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			'Response.Redirect( Constants.WELCOME_MSG + "?e=" + Constants.NEW_USER_REGISTERED + "&" + Constants.REGISTRATION_PARAM + "=" + Constants.REGISTRATION_VALUE );
			Response.Redirect(Constants.WELCOME)
		End Sub

		Private Sub valFileFormat_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)

		End Sub

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable uploadImageForm.
		====================
		Public ReadOnly Property uploadImageForm() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated_uploadImageForm
			End Get
		End Property
		'set { Migrated_uploadImageForm = value; }
	End Class
End Namespace






